package com.fram.controller.api;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.user.request.H5BindPhoneRequest;
import com.fram.bean.api.user.request.H5WechatLoginRequest;
import com.fram.bean.api.user.request.PhoneLoginRequest;
import com.fram.bean.api.user.request.SendVerificationCodeRequest;
import com.fram.bean.api.user.request.UpdatePasswordRequest;
import com.fram.bean.api.user.request.UserUpdateRequest;
import com.fram.bean.api.user.request.XcxBindPhoneRequest;
import com.fram.bean.api.user.request.XcxLoginRequest;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.canstant.PublicConstant;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.IntegralTransactionFlow;
import com.fram.service.api.v1.ApiUserService;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月27日 - 上午9:09:37 @description：-> 用户模块API
 */
@RestController
@RequestMapping("/api/{version}/user")
public class UserController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(UserController.class);

	@Resource(name = "apiUserService")
	private ApiUserService apiUserService;
	@Resource(name = "apiUserServiceV2")
	private ApiUserService apiUserServiceV2;

	/** 小程序登录 */
	@ApiVersion(1.0)
	@RequestMapping("xcxLogin")
	public ResponseMsg xcxLogin(@RequestBody XcxLoginRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.xcxLogin(requestParam));
		} catch (ProAffairException e) {
			LOG.error("-用户-小程序登录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-小程序登录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 手机号登录 */
	@ApiVersion(1.0)
	@RequestMapping("phoneLogin")
	public ResponseMsg phoneLogin(@RequestBody PhoneLoginRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.phoneLogin(requestParam));
		} catch (ProAffairException e) {
			LOG.error("-用户-手机号登录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-手机号登录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改资料 */
	@ApiVersion(1.0)
	@RequestMapping("auth/updateUser")
	public ResponseMsg updateUser(@RequestBody UserUpdateRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			requestParam.setUserId(getApiUserId());
			msg.setData(apiUserService.updateUser(requestParam));
		} catch (ProAffairException e) {
			LOG.error("-用户-修改资料->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-修改资料->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 绑定手机号 */
	@ApiVersion(1.0)
	@RequestMapping("auth/bindPhoneNumber")
	public ResponseMsg bindPhoneNumber(String phone, String code) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.bindPhoneNumber(getApiUserId(), phone, code));
		} catch (ProAffairException e) {
			LOG.error("-用户-绑定手机号->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-绑定手机号->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 发送验证码 */
	@ApiVersion(1.0)
	@RequestMapping("sendVerificationCode")
	public ResponseMsg sendVerificationCode(@RequestBody SendVerificationCodeRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getType() == PublicConstant.VERIFICATION_CODE_TYPE_1 || request.getType() == PublicConstant.VERIFICATION_CODE_TYPE_3) {
				request.setUserId(getApiUserId());
			}
			request.setIp(PublicMethod.getIp(PublicMethod.getRequest()));
			apiUserService.sendVerificationCode(request);
		} catch (ProException e) {
			LOG.error("-用户-发送验证码->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (ProAffairException e) {
			LOG.error("-用户-发送验证码->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-发送验证码->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** h5登录 */
	@ApiVersion(1.0)
	@RequestMapping("h5WechatLogin")
	public ResponseMsg h5WechatLogin(@RequestBody H5WechatLoginRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.h5WechatLogin(requestParam));
		} catch (ProAffairException e) {
			LOG.error("-用户-h5登录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-h5登录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 获取用户资料 */
	@ApiVersion(1.0)
	@RequestMapping("auth/userMessage")
	public ResponseMsg userMessage() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.userMessage(getApiUserId()));
		} catch (ProAffairException e) {
			LOG.error("-用户-获取用户资料->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-获取用户资料->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("userMessage")
	public ResponseMsg userMessageNoLogin(Long userId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.userMessage(userId));
		} catch (ProAffairException e) {
			LOG.error("-用户-获取用户资料userMessageNoLogin->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-获取用户资料userMessageNoLogin->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 个人总积分 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/myintegral", method = RequestMethod.POST)
	private ResponseMsg myIntegral() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.myIntegral(getApiUserId()));
		} catch (ProAffairException e) {
			LOG.error("-个人积分->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-个人积分-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 个人积分记录（可根据 积分来源查询 如 签到 resType=7） **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/myintegralrecord", method = RequestMethod.POST)
	private ResponseMsg myIntegralRecord(@RequestBody IntegralTransactionFlow record) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.myIntegralRecord(record));
		} catch (ProAffairException e) {
			LOG.error("-个人积分记录->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-个人积分记录-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 手机号密码注册 */
	@ApiVersion(1.0)
	@RequestMapping("register")
	public ResponseMsg register(@RequestBody PhoneLoginRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			apiUserService.register(requestParam);
		} catch (ProAffairException e) {
			LOG.error("-用户-手机号密码注册出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-手机号密码注册异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改密码 */
	@ApiVersion(1.0)
	@RequestMapping("auth/update/password")
	public ResponseMsg updatePassword(@RequestBody UpdatePasswordRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setLoginRedisKey(getApiLoginRedisKey());
			apiUserService.updatePassword(request);
			msg.setReturnMsg("修改成功，请重新登录");
		} catch (ProAffairException e) {
			LOG.error("-用户-修改密码出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-修改密码异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 小程序登录V2 */
	@ApiVersion(2.0)
	@RequestMapping("xcxLogin")
	public ResponseMsg xcxLoginV2(@RequestBody XcxLoginRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserServiceV2.xcxLogin(requestParam));
		} catch (ProAffairException e) {
			LOG.error("-用户-小程序登录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-小程序登录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 小程序授权手机号V2 */
	@ApiVersion(2.0)
	@RequestMapping("xcxBindPhone")
	public ResponseMsg xcxBindPhone(@RequestBody XcxBindPhoneRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.xcxBindPhone(request));
		} catch (ProAffairException e) {
			LOG.error("-用户-小程序绑定手机号出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-小程序绑定手机号异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** h5 v2登录 */
	@ApiVersion(2.0)
	@RequestMapping("h5WechatLogin")
	public ResponseMsg h5WechatLoginV2(@RequestBody H5WechatLoginRequest requestParam) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserServiceV2.h5WechatLogin(requestParam));
		} catch (ProAffairException e) {
			LOG.error("-用户-h5 V2登录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-h5 V2登录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** h5绑定手机号 */
	@ApiVersion(2.0)
	@RequestMapping("h5BindPhone")
	public ResponseMsg h5BindPhone(@RequestBody H5BindPhoneRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(apiUserService.h5BindPhone(request));
		} catch (ProAffairException e) {
			LOG.error("-用户-h5绑定手机号出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户-h5绑定手机号异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
